import {Base} from "../../utils/base.js";
class Order extends Base{
   constructor(){
      super();
      this._storageKeyName = 'newOrder';
   }
   //获取定单的具体内容
   getOrderInfoById(id, callback){
      var that = this;
      var allParams = {
         url: 'order/' + id,
         scallback:function(res){
            callback && callback(data);
         },
         ecallback:function(){}
      }
      this.request(allParams);
   }
   //下定单
   doOrder(orderInfo, callback){
      var that = this;
      var params = {
         url: 'order',
         type: 'post',
         data: { products: orderInfo},
         scallback: function(res){
            that.execSetStorageSync(true);
            callback && callback(res);
         },
         ecallback:function(){}
      }
      this.request(params);
   }
   //本地缓存，保存/更新
   execSetStorageSync(data){
      wx.setStorageSync(this._storageKeyName, data);
   }
   //拉起发起支付 orderNumber是定单号 callback是回调方法，返回参数 可能值是0， 商品缺货等原因导致订单不能支付; 1:支付失败或者支付取消； 2支付成功！
   execPay(orderNumber, callback){
      var allParams = {
         url: 'pay/pre_order',
         type: 'post',
         data: {id: orderNumber},
         scallback:function(data){
            //如果服务器正常，则timeStamp有值
            var timeStamp = data.timeStamp;
            if(timeStamp){
               wx.requestPayment({
                  timeStamp: timeStamp.toString(),
                  nonceStr: data.nonceStr,
                  package: data.package,
                  signType: data.signType,
                  paySign: data.paySign,
                  success:function(){
                     callback && callback(2);
                  },
                  fail: function(){
                     callback && callback(1);
                  }
               });
            }else{
               callback && callback(0);
            }
         }
      };
   }
}
export {Order};